package com.dullfox.sbm.service.impl;

import com.dullfox.sbm.dao.ext.SysUserMapperExt;
import com.dullfox.sbm.model.SysUser;
import com.dullfox.sbm.model.vo.SysUserVo;
import com.github.pagehelper.PageHelper;
import com.dullfox.sbm.common.bean.PageResult;
import com.dullfox.sbm.common.util.ConstUtil;
import com.dullfox.sbm.common.util.UUIDUtil;
import com.dullfox.sbm.dao.SysUserMapper;
import com.dullfox.sbm.service.ISysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * Author: zengdy
 * Date: 2018/10/25
 */
@Service
@Transactional
public class SysUserService implements ISysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysUserMapperExt sysUserMapperExt;

    @Override
    public void save(SysUser sysUser) {
        String id = sysUser.getId();
        if (StringUtils.isBlank(id)) {
            sysUser.setId(UUIDUtil.generateUUID());
            sysUser.setStatus(ConstUtil.STATUS.NORMAL.getStatus());
            sysUser.setCreateAt(new Date());
            sysUserMapper.insertSelective(sysUser);
        } else {
            sysUserMapper.updateByPrimaryKeySelective(sysUser);
        }
    }

    @Override
    public PageResult<SysUser> listPage(SysUserVo sysUserVo) {
        PageHelper.startPage(sysUserVo.getPageNo(), sysUserVo.getPageSize());
        return new PageResult<SysUser>(sysUserMapperExt.selectByPage(sysUserVo));
    }

    @Override
    public SysUser login(SysUserVo sysUserVo) {
        List<SysUser> list = sysUserMapperExt.login(sysUserVo);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        return list.get(0);
    }

}
